package org.krams.domain;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity(name = "user")
public class User {

	public static final int STATUS_DELETED = 0;
	public static final int STATUS_ENTERED = 100;
	public static final int STATUS_READY = 200;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	private String fullname_ru;
	private String fullname_en;

	@Column(unique = true)
	private String username;
	private String password;

	@OneToMany(mappedBy = "user", cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
	private List<Role> roles;

	private Integer status_;
	private String locale;
	private String location;
	private String email;
	private String phone;
	@Lob
	private byte[] avatar;
	private String avatarType;

	@Temporal(TemporalType.TIMESTAMP)
	private Date updTime;

	//	@ManyToOne
	//	@JoinColumn(name = "parent_id", updatable = false)
	//	private Comment parentComment;
	//
	//	@OneToMany(mappedBy = "parentComment", fetch = FetchType.LAZY)
	//	@Cascade({ org.hibernate.annotations.CascadeType.DELETE })
	//	private List<Comment> childComments;

	@ManyToOne
	@JoinColumn(name = "updUser")
	private User updUser;

	@OneToMany(mappedBy = "updUser", fetch = FetchType.LAZY)
	//@Cascade({ CascadeType.DELETE })
	private List<User> childUsers;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public String getFullname_ru() {
		return fullname_ru;
	}

	public void setFullname_ru(String fullname_ru) {
		this.fullname_ru = fullname_ru;
	}

	public String getFullname_en() {
		return fullname_en;
	}

	public void setFullname_en(String fullname_en) {
		this.fullname_en = fullname_en;
	}

	public Integer getStatus_() {
		return status_;
	}

	public void setStatus_(Integer status_) {
		this.status_ = status_;
	}

	public String getLocale() {
		return locale;
	}

	public void setLocale(String locale) {
		this.locale = locale;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public byte[] getAvatar() {
		return avatar;
	}

	public void setAvatar(byte[] avatar) {
		this.avatar = avatar;
	}

	public String getAvatarType() {
		return avatarType;
	}

	public void setAvatarType(String avatarType) {
		this.avatarType = avatarType;
	}

	public Date getUpdTime() {
		return updTime;
	}

	public void setUpdTime(Date updTime) {
		this.updTime = updTime;
	}

	public User getUpdUser() {
		return updUser;
	}

	public void setUpdUser(User updUser) {
		this.updUser = updUser;
	}

	public List<User> getChildUsers() {
		return childUsers;
	}

	public void setChildUsers(List<User> childUsers) {
		this.childUsers = childUsers;
	}

	public boolean getEnabled() {
		if (status_ != null && status_ == STATUS_READY) {
			return true;
		}
		return false;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", status_=" + status_ + "]";
	}
}
